/*
 * =====================================================================================
 *
 *       Filename:  moonlander.cpp
 *
 *    Description:  
 *
 *        Version:  1.0
 *        Created:  22.01.2010 12:22:21
 *       Revision:  none
 *       Compiler:  gcc
 *
 *         Author:  Michal Antonczak (uzyszkodnik), uzyszkodnik.priv@gmail.com
 *        Company:  
 *
 * =====================================================================================
 */
#include 	"moonlander.h"
#include	"lander.h"
#include	"gamestructs.h"
#include	"map.h"
#include	"timer.h"
#include	<SDL/SDL.h>
#include	<SDL/SDL_ttf.h>
#include	<cstdlib>>

using namespace std;
void Moonlander::gameinit(char* mapz, SDL_Surface *scr){

	msg = NULL;

	screen = scr;

	SDL_EnableKeyRepeat(1,FRAME_TIME);

	g_map  = new Game_Map(mapz);

	player = new Lander(screen,g_map->collision_map,&g_map->phys,&g_map->ship_info, &g_map->posi);
	
	map_path = mapz;	

	textColor = { 0, 0xFF, 0 }; 

	if((font = TTF_OpenFont( "font.ttf", 28 ))==NULL){

		printf("ttf init: %s", TTF_GetError());
		exit(1);

	}

	
}
Moonlander::~Moonlander(){
	delete g_map;
	delete player;
	if(msg!=NULL)
		SDL_FreeSurface(msg);
	TTF_CloseFont(font);

}
void Moonlander::reinit(){
	delete g_map;
	delete player;

	if(msg!=NULL)
		SDL_FreeSurface(msg);

	g_map  = new Game_Map(map_path);
	player = new Lander(screen, g_map->collision_map,&g_map->phys,&g_map->ship_info, &g_map->posi);

	msg = NULL;	

}
void Moonlander::sdlinit(){

	

	if(SDL_Init( SDL_INIT_TIMER | SDL_INIT_VIDEO)<0){
		printf("sdl init error: %s", SDL_GetError());	
		exit(1);
	}
	if(TTF_Init()<0){
		printf("ttf init: %s", TTF_GetError());
		exit(1);
	}
	
	SDL_WM_SetCaption("llc",NULL);

	if((font = TTF_OpenFont( "font.ttf", 28 ))==NULL){

		printf("ttf init: %s", TTF_GetError());
		exit(1);

	} 

/*


	img_flags = IMG_INIT_JPG;

	if((IMG_INIT(flags)&flags)!=flags)
		sdlimgerror("SDL_IMG initialization");
		
*/
	atexit(SDL_Quit);
	if((screen = SDL_SetVideoMode(800,600, 32, SDL_DOUBLEBUF | SDL_HWSURFACE))== NULL)
		sdlerror("video initialization");	
	SDL_EnableKeyRepeat(1,FRAME_TIME);



}


void Moonlander::mainloop(){
SDL_Event event;
Timer timer;
Uint32 frame_time;
int game_pause=0;
//Uint8* keyboard = SDL_GetKeyState(NULL);

start:
	while(1){
		
		timer.start();
		dispatch_events:
		if(game_pause)
			while(SDL_PollEvent(&event))
				switch(event.type){
					case SDL_QUIT:
						exit(1);
						break;

					case SDL_KEYDOWN:
						switch(event.key.keysym.sym){
							case SDLK_p:
								if(event.key.state==SDL_PRESSED){
									game_pause^= 1;
									goto normal_loop;
								}
								break;
							case SDLK_q:
								if(event.key.state==SDL_PRESSED)
									return;
								break;
							case SDLK_r:
								if(event.key.state==SDL_PRESSED){
									reinit();
									game_pause=0;
									goto start;
								}
								break;

					}
					break;	

				}
		normal_loop:
		while(SDL_PollEvent(&event)){
			switch(event.type){
				case SDL_QUIT:
					exit(1);
					break;

				case SDL_KEYDOWN:
					switch(event.key.keysym.sym){
						case SDLK_p:
								game_pause^= 1;
								if(game_pause)
									goto dispatch_events;
							break;
						case SDLK_s:
								player->incengine();
							break; 
						case SDLK_a:
								player->rotate_left();
							break;
						case SDLK_d:
								player->rotate_right();
							break;
						case SDLK_q:
								return;
							break;
						case SDLK_r:
								reinit();
								game_pause==0;
								goto start;
							break;
								

					}
					break;	

				case SDL_KEYUP:
					switch(event.key.keysym.sym){
						case SDLK_s:
							if(event.key.state==SDL_RELEASED)
								player->stopengine();
							break;

					}
					break;
			}

	
 		}
		/*		
		map->show();
		player->show();
		*/
		if(!game_pause){
			player->frame();
			switch(player->state){

				case SHIP_CRASHED:
					crashed();
					game_pause=1;
					break;
				case SHIP_LANDED:
					landed();
					game_pause=1;
					break;
			}
		}
		camera();

		SDL_Flip(screen);
		
		//frame speed regulation
		frame_time = timer.timess();
		if(frame_time<FRAME_TIME)
			SDL_Delay(FRAME_TIME-frame_time);
//		else
//			exit(1);
	}

}

void Moonlander::camera(){
	
	struct position_i middle;
	SDL_Rect pos,surface;
	SDL_Rect hud;
	//coords of img in the middle of the screen
	middle.x = (800-player->lander_tmp->w)/2;
	middle.y=  (600-player->lander_tmp->h)/2;	
	// if the img can't be in the middle of the screen
	
	pos.x=middle.x;
	pos.y=middle.y;
		
	surface.x=player->position_int.x-middle.x;
	surface.y=player->position_int.y-middle.y;
	surface.w=800;
	surface.h=600;

	//if we can't display lander in the middle, coz we'll cause segfault
	if(player->position_int.x<middle.x){
		pos.x=player->position_int.x;
		surface.x=0;
	}
	if(player->position_int.y<middle.y){
		pos.y=player->position_int.y;
		surface.y=0;	
	}	
	if( player->position_int.x >= ( g_map->img->w+middle.x-800 ) ){
		surface.x=g_map->img->w-800;
		pos.x=player->position_int.x-surface.x;
	
	}

	if( player->position_int.y >= (g_map->img->h+middle.y-600)){

		surface.y=g_map->img->h-600;

		pos.y=player->position_int.y-surface.y;
	}




	int tmpx, tmpy ;
	tmpx = pos.x - player->communicate->w;
	tmpy = pos.y-(player->communicate->h-player->lander_tmp->h)/2;


	if(tmpx<0)
		hud.x = player->lander_tmp->w+pos.x;
	else
		hud.x=tmpx;

	if(tmpy<0)
		tmpy=0;
	else if(tmpy> ( 600-player->communicate->h))
		tmpy=600-player->communicate->h;

	hud.y = tmpy;


	SDL_BlitSurface(g_map->img, &surface, screen, NULL); 

	player->pengine->display(surface.x,surface.y);

	SDL_BlitSurface(player->lander_tmp, NULL, screen, &pos);	

	

	SDL_BlitSurface(player->communicate,NULL, screen, &hud);

	if(msg!=NULL){
		SDL_Rect coords;
		coords.x=200;
		coords.y=300;
		SDL_BlitSurface(msg,NULL,screen, &coords);		
	}

}

void Moonlander::crashed(){
		
	if(msg!=NULL)
		SDL_FreeSurface(msg);

	msg = TTF_RenderText_Solid( font, "You failed press r to play again or q to quit", textColor );  

}
void Moonlander::landed(){

	if(msg!=NULL)
		SDL_FreeSurface(msg);

	msg = TTF_RenderText_Solid( font, "Gratz u've succesfully landed", textColor ); 

}
